package com.by.service.impl;

import com.by.PageBean.SpuResult;
import com.by.common.dao.MyBaseServiceImpl;
import com.by.common.dao.bean.Page;
import com.by.entity.*;
import com.by.mapper.*;
import com.by.service.SpuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import static com.by.common.core.utils.Assert.isFail;

import java.util.List;
import java.util.Map;

@Service
public class SpuServiceImpl extends MyBaseServiceImpl<Spu, SpuMapper> implements SpuService {

    @Autowired
    SpuMapper spuMapper;

    @Autowired
    SkuMapper skuMapper;

    @Autowired
    DetailMapper detailMapper;

    @Autowired
    AttrMapper attrMapper;

    @Autowired
    AttrValueMapper attrValueMapper;

    @Autowired
    SpuAttrValueMapper spuAttrValueMapper;

    @Autowired
    SkuStockMapper skuStockMapper;

    @Autowired
    SpuExtendMapper spuExtendMapper;

    @Autowired
    SpuSkuAttrValueMapper spuSkuAttrValueMapper;


    //出现异常回滚
    @Transactional
    public int addProduct(Spu spu) {
        //先保存进spu表中
        int rs = spuMapper.insert(spu);
        isFail(rs, "spu保存失败");
        //保存到商品详情表中
        Detail detail = new Detail(spu.getSpuId(), spu.getDetail());
        rs = detailMapper.insert(detail);
        isFail(rs, "Detail保存失败");
        //定义库存数量
        int stock = 0;
        if (null != spu.getAttList() && !spu.getAttList().isEmpty()) {
            //遍历集合保存到sku表中
            List<Sku> skuList = spu.getSkuList();
            for (Sku s : skuList) {
                //累加库存
                stock += s.getActual_stock();
                //保存到sku中
                s.setSpuId(spu.getSpuId());
                int rrr = skuMapper.insert(s);
                isFail(rrr, "sku保存失败");
                //保存库存
                SkuStock skuStock = new SkuStock();
                skuStock.setSkuId(s.getSkuId());
                skuStock.setActualStock(s.getActual_stock());
                rs = skuStockMapper.insert(skuStock);
                //如果返回0就报错然后回滚
                isFail(rs, "保存sku库存失败");
            }
        }
        SpuExtend spuExtend = new SpuExtend();
        spuExtend.setSpuId(spu.getSpuId());
        spuExtend.setActualStock(stock);
        rs = spuExtendMapper.insert(spuExtend);
        isFail(rs, "扩展保存失败");
        if (null != spu.getAttList() && spu.getAttList().size() > 0) {
            List<Attr> attrList = spu.getAttList();
            //遍历attlist集合获取attrValue并存入表中
            for (Attr attr : attrList) {
                //保存到属性信息表中
                int temp = attrMapper.insert(attr);
                isFail(temp, "属性保存失败");
                //获取商品属性值集合 存入表attrValue中
                if (null != attr.getAttValueList() && attr.getAttValueList().size() > 0) {
                    temp = attrValueMapper.addAttrValues(attr.getAttValueList(), attr.getAttrId());
                    isFail(temp, "属性值保存失败");
                }
                //存入spuAttrValue表
                int apu = spuAttrValueMapper.save(spu.getSpuId(), attr.getAttrId());
                isFail(apu, "spuAttrValue保存失败");
                int rss = spuSkuAttrValueMapper.save(spu.getSpuId());
                isFail(rss, "spuSkuAttrValue保存失败");

            }
        }

        return 1;

    }
    //根据id查询
    public Map getSpu(int id){
        return spuMapper.get(id);
    }
    //无条件查询
    public SpuResult getSpuAll(Page page){
        //分页信息
        PageHelper.startPage(page.getPage(),page.getPageSize());
        //查询全部
        List<Spu> list =spuMapper.getAll();
        //分页
        PageInfo<Spu> p=new PageInfo(list);
        SpuResult ss=new SpuResult();
        ss.setPage(page.getPage());
        ss.setPageSize(page.getPageSize());
        ss.setTotal(p.getTotal());
        ss.setData(p.getList());
        return ss;
    }
    //根据spuId查看规格属性

}
